#include "copyright.h"
#include "system.h"
#include "syscall.h"
#include "bitmap.h"
#include "addrspace.h"
#include "pageprovider.h"

BitMap *physicalPageTable;

PageProvider::PageProvider(int pages) {
  physicalPageTable = new BitMap(pages);
}

PageProvider::~PageProvider() {
  delete physicalPageTable;
}

int
PageProvider::GetEmptyPage() {
  int slot = physicalPageTable->Find();
  if (slot != -1) {
    physicalPageTable->Mark(slot);
    //init page 
    memset(&machine->mainMemory[slot * PageSize], 0, PageSize);
  }
  return slot;
}

void
PageProvider::ReleasePage(int slot) {
  physicalPageTable->Clear(slot);
}

int
PageProvider::NumAvailPage() {
  return physicalPageTable->NumClear();
}